package CGW.controller;

import CGW.bean.Dinner_table;
import CGW.bean.Page;
import CGW.dao.BaseDao;
import CGW.dao.Dinner_tableDao;
import CGW.service.Dinner_tableService;
import CGW.utils.ZHI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/dinner_table1")
public class YouKe_Dinner_tableServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dinner_tableService service = new Dinner_tableService(new Dinner_tableDao());
        String action = req.getParameter("action");
        if (action == null){
            String page = req.getParameter("page");
            int pageInt = 1;
            String pageSize = req.getParameter("pageSize");
            int pageSizeInt = 100;
            if(page != null){
                pageInt = Integer.parseInt(page);
            }
            if (pageSize != null){
                pageSizeInt = Integer.parseInt(pageSize);
            }

            Page<Dinner_table> pageBean1= service.queryAll(pageInt, pageSizeInt);
            System.out.println(pageBean1.getDatas());
            req.setAttribute("pageBean1",pageBean1);
            req.getRequestDispatcher("table.jsp").forward(req,resp);

        }else if(action.equals("update")){
            String table_id = req.getParameter("table_id");
            String table_status = req.getParameter("table_status");
            if (table_status.equals("1")){
                Dinner_table dinner_table = new Dinner_table(ZHI.zh(table_id),ZHI.zh(table_status));
                BaseDao baseDao = new BaseDao();
                int ret = baseDao.update_one(dinner_table);
                baseDao.commit();
                req.setAttribute("dinner_table1", dinner_table);
                resp.sendRedirect("/dinner_table1");
            }
            if (table_status.equals("0")){
                Dinner_table dinner_table = new Dinner_table(ZHI.zh(table_id),ZHI.zh(table_status));
                BaseDao baseDao = new BaseDao();
                int ret = baseDao.update_one(dinner_table);
                baseDao.commit();
                req.setAttribute("dinner_table1", dinner_table);
                resp.sendRedirect("/dinner_table1");
            }


        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
